interface IaddressScope extends ng.IScope {
    /**
     * 获取省数据下拉
     */
    getProvinceInfo():void;
    /**
     * 省数据下拉
     */
    provinceList:lineAddress[];
    /**
     * 市数据下拉
     */
    cityList:lineAddress[];
    /**
     * 区数据下拉
     */
    areaList:lineAddress[];
    /**
     * 获取市数据下拉
     */
    changeProvince():void;
    /**
     * 获取区数据下拉
     */
    changeCity():void;
    /**
     * 改变区出发事件
     */
    changeArea():void;
    province:string;
    city:string;
    area:string;
    /**
     * 是否进行验证(不验证为-1)
     */
    required:boolean;
    /**
     * 是否出发下拉选中
     */
    isSelected:boolean;
    ischecked:boolean;
}
interface lineAddress{
  text:string;
  value:string;
}

function tmsAddress( areaService: IAreaService,$ngBootbox:BootboxService): ng.IDirective{
    return {
        templateUrl:'./src/directives/tms-address/tms-address.html',
        scope : {
            province: '=',
            city: '=',
            area: '=',
            cityList: '=',
            areaList: '=',
            isSelected: '=',
            required: '=',
            ischecked:"=",
            isDisabled:"="
        },
        link  : ($scope: IaddressScope) =>{
           if(!$scope.required){$scope.province="-1";$scope.city = "-1";$scope.area="-1"};
            areaService.getArea().then(res=>{
                $scope.provinceList = res.data.data;
            });

            $scope.changeProvince = ()=>{
                if(!$scope.province||$scope.province===""){
                    $scope.cityList.length=0;
                    if($scope.areaList!=undefined){
                        $scope.areaList.length=0;
                    }
                    
                    if(!$scope.required){$scope.province="-1";$scope.area="-1";$scope.city="-1";};
                    return
                };
               areaService.getCity($scope.province).then(res=>{
                   $scope.cityList = res.data.data;
               });
            }

            $scope.changeCity = ()=>{
                if(!$scope.city||$scope.city===""){
                    if(!$scope.required){$scope.city="-1";};
                    $scope.areaList.length=0;
                    return;
                };
                areaService.getCounty($scope.city).then(res=>{
                    $scope.areaList = res.data.data;
                })
            }

            $scope.changeArea = ()=>{
                if(!$scope.area||$scope.area===""){
                     if(!$scope.required){$scope.area="-1";};
                }
            };

            //是否自动选中
            if($scope.ischecked){
                var watchProvince = $scope.$watch("province",()=>{
                    if( $scope.province && $scope.province!=="" && $scope.province!=="-1"){
                        areaService.getCity($scope.province).then(res=>{
                            $scope.cityList = res.data.data;
                        });
                        areaService.getCounty($scope.city).then(res=>{
                                $scope.areaList = res.data.data;
                        });
                        //watch();
                    }
                });

                var watchCity = $scope.$watch("city",()=>{
                    if( $scope.city && $scope.city!=="" && $scope.city!=="-1"){
                        areaService.getCounty($scope.city).then(res=>{
                                $scope.areaList = res.data.data;
                        });
                    }
                });
            }

         
        }
    };
}


tmsAddress['$inject'] = ["areaService","$ngBootbox"]
angular.module('tmsAddress', []).directive('tmsAddress', tmsAddress);






